<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('估值时间')" prop="valuationTime">
            <DatePicker v-model="form.valuationTime" :placeholder="$t('估值时间')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('本金') + $t('金额单位')" prop="principal">
            <Money v-model="form.principal" :placeholder="$t('本金')" unit="w" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('利息') + $t('金额单位')" prop="income">
            <Money v-model="form.income" :placeholder="$t('利息')" unit="w" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('估值金额') + $t('金额单位')" prop="valuationAmount">
            <Money v-model="form.valuationAmount" :placeholder="$t('估值金额')" unit="w" :disabled="true" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('备注')" prop="remark">
            <el-input v-model="form.remark" type="textarea" :placeholder="$t('备注')" show-word-limit maxlength="2000" />
          </el-form-item>
        </el-col>
      </el-row>

      <!--签约主体-->
      <div v-loading="investLoading">
        <div style="margin-top:15px;">
          <div class="area-header">
            <span class="area-header-title">{{ $t('出资主体列表') }}</span>
          </div>
          <el-table :data="form.investList" border class="nowrapTable formTable">
            <el-table-column :label="$t('主体类型')" align="center">
              <template slot="header" slot-scope="{}">
                <span>{{ $t('主体类型') }}</span>
              </template>
              <template slot-scope="scope">
                <el-form-item :prop="'investList.' + scope.$index + '.investType'">
                  <dictSelect
                    v-model="scope.row.investType"
                    :clearable="true"
                    dictionary="main_body_type"
                    :disabled="true"
                  />
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column :label="$t('主体名称')" align="center">
              <template slot="header" slot-scope="{}">
                <span>{{ $t('主体名称') }}</span>
              </template>
              <template slot-scope="scope">
                <el-form-item :prop="'investList.' + scope.$index + '.investMain'">
                  <MainBodySelect
                    v-model="scope.row.investMain"
                    :parameters="scope.row.investType"
                    :disabled="true"
                  />
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column :label="$t('投资金额') + $t('金额单位')" align="center">
              <template slot="header" slot-scope="{}">
                <span>{{ $t('投资金额') }}{{ $t('金额单位') }}</span>
              </template>
              <template slot-scope="scope">
                <el-form-item :prop="'investList.' + scope.$index + '.investAmount'">
                  <Money v-model="scope.row.investAmount" :disabled="true" unit="w" />
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column :label="$t('预计回款本金') + $t('金额单位')" align="center">
              <template slot="header" slot-scope="{}">
                <i style="color: #ff4949;margin-right: 4px;">*</i>
                <span>{{ $t('预计回款本金') }}{{ $t('金额单位') }}</span>
              </template>
              <template slot-scope="scope">
                <el-form-item :prop="'investList.' + scope.$index + '.hkPrincipal'" :rules="investListRules.hkPrincipal">
                  <Money v-model="scope.row.hkPrincipal" unit="w" @change="calcInvestAmount" />
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column :label="$t('预计回款利息') + $t('金额单位')" align="center">
              <template slot="header" slot-scope="{}">
                <i style="color: #ff4949;margin-right: 4px;">*</i>
                <span>{{ $t('预计回款利息') }}{{ $t('金额单位') }}</span>
              </template>
              <template slot-scope="scope">
                <el-form-item :prop="'investList.' + scope.$index + '.hkIncome'" :rules="investListRules.hkIncome">
                  <Money v-model="scope.row.hkIncome" unit="w" @change="calcInvestAmount" />
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column :label="$t('估值金额') + $t('金额单位')" align="center">
              <template slot="header" slot-scope="{}">
                <span>{{ $t('估值金额') }}{{ $t('金额单位') }}</span>
              </template>
              <template slot-scope="scope">
                <el-form-item :prop="'investList.' + scope.$index + '.valuationAmount'">
                  <Money v-model="scope.row.valuationAmount" :disabled="true" unit="w" />
                </el-form-item>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </div>
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存 取消按钮-->
      <save-btn has-permi="debt:valuation" />
    </div>
  </div>
</template>

<script>
    import { saveDealDebtValuation, getDealDebtValuation } from '@/api/deal/valuation/dealDebtValuation'
    import { findInvestMain } from '@/api/bf/contract/bfContract'
    import MainBodySelect from '@/components/MainBodySelect'
    export default {
        components: {
          MainBodySelect
        },
        data() {
          const checkInvestList = (rule, value, callback) => {
            if (rule.field.indexOf('hkIncome') !== -1) {
              value = this.form.investList[parseInt(rule.field.replace(/[^0-9]/ig, ''))].hkIncome
            }
            if (rule.field.indexOf('hkPrincipal') !== -1) {
              value = this.form.investList[parseInt(rule.field.replace(/[^0-9]/ig, ''))].hkPrincipal
            }
            if (value === undefined || value === '' || value === null) {
              callback(new Error(this.$t('必填信息')))
            } else {
              callback()
            }
          }
            return {
                // 表单参数
                form: {},
                // 项目ID
                dealId: this.$route.query.id,
                // 遮罩层
                loading: false,
                // 出资主体列表
                investLoading: false,
                // 表单校验
                rules: {
                  valuationTime: [{ required: true, message: this.$t('必填信息'),	trigger: 'blur' }],
                  principal: [{ required: true, message: this.$t('必填信息'),	trigger: 'blur' }],
                  income: [{ required: true, message: this.$t('必填信息'),	trigger: 'blur' }]
                },
                investListRules: {
                  hkPrincipal: [{ required: true, validator: checkInvestList,	trigger: 'blur' }],
                  hkIncome: [{ required: true, validator: checkInvestList,	trigger: 'blur' }]
                }
            }
        },
        watch: {
          'form.principal'() {
            this.calcAmount()
          },
          'form.income'() {
            this.calcAmount()
          }
        },
        created() {
        },
        methods: {
            // 初始化数据
            init(data) {
                if (data != null && data.id !== undefined) {
                    getDealDebtValuation(data.id).then(response => {
                        this.form = response.data
                    })
                } else {
                    this.reset()
                    // 出资主体带入默认值
                    this.investLoading = true
                    findInvestMain(this.dealId).then(response => {
                      this.form.investList = response.data
                      this.investLoading = false
                    })
                }
            },
            // 计算估值金额
            calcAmount() {
              if (this.form.principal !== undefined && this.form.income !== undefined) {
                this.form.valuationAmount = this.form.principal + this.form.income
              }
            },
            // 计算出资主体估值金额
            calcInvestAmount() {
              if (this.form.investList !== undefined && this.form.investList.length > 0) {
                // 计算估值金额
                this.form.investList.forEach((item) => {
                  if (item.hkPrincipal !== undefined && item.hkIncome !== undefined) {
                    item.valuationAmount = item.hkPrincipal + item.hkIncome
                  }
                })
              }
            },
            // 表单重置
            reset() {
                this.form = {
                    id: undefined,
                    dealId: this.$route.query.id,
                    valuationTime: undefined,
                    principal: undefined,
                    income: undefined,
                    valuationAmount: undefined,
                    latest: undefined,
                    createBy: undefined,
                    createTime: undefined,
                    updateBy: undefined,
                    updateTime: undefined,
                    delFlag: undefined,
                    remark: undefined
                }
                this.resetForm('form')
            },
            // 取消按钮
            cancel() {
                this.$parent.$parent.formOpen = false
                // 债权首页添加估值后
                if (this.$parent.$parent.$parent.valuationOpen !== undefined) {
                  this.$parent.$parent.$parent.valuationOpen = false
                  this.$parent.$parent.$parent.valuationHandler(this.$parent.$parent.$parent.statistical)
                } else {
                  this.$parent.$parent.reset()
                }
            },
            // 关闭form页面
            closeForm() {
                // 关闭form页面遮罩层
                this.loading = false
                // 关闭form页面
                this.$parent.$parent.formOpen = false
                if (this.$parent.$parent.$parent.valuationOpen !== undefined) {
                  this.$parent.$parent.$parent.valuationOpen = false
                }
                // 刷新list页面
                this.$parent.$parent.getList()
            },
            /** 提交按钮 */
            submitForm: function() {
                this.$refs['form'].validate(valid => {
                    if (valid) {
                        this.loading = true
                        saveDealDebtValuation(this.form).then(response => {
                            if (response.code === 200) {
                                this.msgSuccess(this.$t('保存成功'))
                                this.closeForm()
                            } else {
                                this.msgError(response.msg)
                            }
                            this.loading = false
                        })
                    } else {
                      this.locationError(document)
                    }
                })
           }

        }

    }
</script>
